/**
 * Created by sxx on 2017/5/1.
 */
import "./index.less";
import "../../../unit/common/js/bootstrap_js/jquery.validate.min.js";
import productEditTpl from './product_edit.handlebars';
import httpurl from "../../../unit/common/js/httpurl";

let token = sessionStorage.getItem('supplier_token');

let prodId = httpurl.uri.params.id;
let ProductEdit = {
    init: function(){
        this.getInfo()
            .then(this.renderList)
            .then(this.bindEvent);
    },
    getInfo: function(){
        return new Promise((resolve, reject)=>{
            $.ajax({
                url: HOST_ADDR+'/cpmp/splrSlfMng/selfProd/'+prodId,
                type: 'get',
                dataType: 'json',
                headers: {
                    'access_token': token
                },
                contentType:"application/json",
                success: function(rsp){
                    console.log(rsp);
                    resolve(rsp);
                }
            })
        });
    },
    renderList: function(rsp){
        let productHtml = productEditTpl(rsp.data);
        $('.J_edit-product-form').html(productHtml);
    },
    bindEvent: function(){
        $('.J_back-to-list').unbind().click(function(){
            location.href = '/supplier_self/product_list.html';
        });
        $('.J_submit').unbind().click(function () {
            console.log('submitsssssssssssssss');
            console.log('submitsssssssssssssss');
            let $form = $('.J_edit-product-form');
            let prodNam = $form.find('.J_prodNam').val();
            let prodBrand = $form.find('.J_prodBrand').val();
            let prodItdc = $form.find('.J_prodItdc').val();
            let prodModel = $form.find('.J_prodModel').val();
            let prodNum = $form.find('.J_prodNum').val();
            let prodPrik = $form.find('.J_prodPrik').val();

            let data = {
                "prodBrand": prodNam,
                "prodItdc": prodItdc,
                "prodModel": prodModel,
                "prodNam": prodNam,
                "prodNum": prodNum,
                "prodPrik": prodPrik,
            };
            console.log(data);

            $.ajax({
                url: HOST_ADDR + '/cpmp/splrSlfMng/selfProd/'+prodId,
                type: 'post',
                data: JSON.stringify(data),
                dataType: 'json',
                contentType:"application/json",
                access_token: token,
                headers: {
                    'access_token': token,
                    'login_type': 'splr'
                },
                success: function(resp){
                    if(resp.success){
                        alert('修改自荐产品成功');
                        location.href = '/supplier_self/product_list.html';
                    }else{
                        alert('修改自荐产品失败');
                    }
                },
                error:function(err){
                    console.log(err);
                }
            });
        });
    }

}
ProductEdit.init();


// $('.J_add-user-form .J_cancel').click(function(){
//     location.href = '/supplier_self/user_manage.html';
// });



